package com.taritari.journal.utils;

import cn.hutool.core.date.DateTime;
import com.taritari.journal.enums.MonthEnum;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author taritari
 * @date 2024-01-09 20:50
 * @description 日期处理的工具类
 */
@Component
public class DateUtil {
    /**
     * 转为时：分格式
     * */
    public String timeFormat(Date date){
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        String time = timeFormat.format(date);
        return time;
    }
    /**
     * 转为日期格式
     * */
    public String dateFormat(Date time){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = dateFormat.format(time);
        return date;
    }
    /**
     * 获取日期的天
     * */
    public String dayFormat(Date date){
        SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
        String dayOfMonth = dayFormat.format(date);
        return dayOfMonth;
    }
    /**
     * 判断星期
     * */
    public String weekFormat(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        String[] daysOfWeek = {"", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        String weekDay = daysOfWeek[dayOfWeek];
        return weekDay;
    }
    /**
     * 单独获取年
     * */
    public String yearFormat(Date time){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
        String year = dateFormat.format(time);
        return year;
    }
    /**
     * 单独获取月
     * */
    public String monthFormat(int month){
        int lastTwoDigits = month % 100;
        // 将结果格式化成字符串，确保有两位数字
        String formattedMonth = String.format("%02d", lastTwoDigits);
        // 通过字符串查找对应的 MonthEnum
        MonthEnum monthEnum = null;
        for (MonthEnum value : MonthEnum.values()) {
            if (value.getMonthNum().equals(formattedMonth)) {
                monthEnum = value;
                break;
            }
        }
        // 现在你可以获得月份的中文名
        String chineseMonth = (monthEnum != null) ? monthEnum.getMonth() : null;
        return chineseMonth;
    }

    /**
     * 	获取两个月份之间的所有月份(含跨年)
     * @param minDate
     * @param maxDate
     * @return
     * @throws ParseException
     */
    public List<String> getMonthBetween(String minDate, String maxDate) {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");// 格式化为年月
        try {
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();

            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }

            // 实现排序方法
            Collections.sort(result, new Comparator<Object>() {
                @Override
                public int compare(Object o1, Object o2) {
                    String str1 = (String) o1;
                    String str2 = (String) o2;
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
                    Date date1 = null;
                    Date date2 = null;
                    try {
                        date1 = format.parse(str1);
                        date2 = format.parse(str2);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    if (date2.compareTo(date1) > 0) {
                        return -1;
                    }
                    return 1;
                }
            });
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 获取前n个月的月份
     * */
    //获取从现在开始倒退n个月之间的年月列表
    public List<String> getMonthBetweenTailNow(int n) {
        Date nowDate = new Date(); //今天的时间
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - (n-1));
        Date beforeDate = calendar.getTime();  //倒退11个月的时间
        //设置时间格式为yyyy-MM
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String now = sdf.format(nowDate);
        String before = sdf.format(beforeDate);
        List<String> monthBetween = getMonthBetween(before, now);
        return monthBetween;
    }
    /**
     * 年月提取月
     * */
    public String yearMonth2Month(String date){

        Pattern p = Pattern.compile("(\\d{4})-(\\d{1,2})");
        Matcher m = p.matcher(date);
        if(m.find()) {

            String month = m.group(2).toString() + "月";
            return month;
        }

        return null;
    }
    /**
     * 获取当前月份 比如 2024-01-14 获取 2401
     * */
    public int getMonth(String dateStr){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(dateStr, formatter);

        int year = date.getYear() % 100; // 获取年份的最后两位数
        int month = date.getMonthValue(); // 获取月份

        String yearStr = String.valueOf(year);
        String monthStr = String.valueOf(month);

        // 格式化月份为两位数，如果是一位数则在前面补0
        if (monthStr.length() == 1) {
            monthStr = "0" + monthStr;
        }
        String formatMonth = yearStr+monthStr;
        return Integer.parseInt(formatMonth);
    }
}
